requirejs.config({
    baseUrl: '../js/libs',
    paths: {
        stringUtil: "../common/string-util",
        commonUtil: '../common/common-util'
    }
});
requirejs(['jquery', 'pageJs','config', 'arttemplateUtil','common','stringUtil','commonUtil','validform'], function($, pageJs,config, arttemplateUtil,common,stringUtil,commonUtil) {
	
	var param = {special_type:2}; //特殊价格类型，默认为活动小区
	var price_tpl;//价格列表模板
	
	var initData = function(){
		var index = common.loading();
		var count;
		common.common_ajax_request('/api/seller/v1/queryCommunityPriceList',param,false,function(data,code,msg){
			if(msg.businessCode==0){
				setData(data);
				pageJs.setPage(data.currentPage,data.totalPages,data.totalCount,callback);			
			}else{
				setData({});
				window.layer.msg("系统异常");
			}
			layer.close(index); //关闭加载层
		},"","post");
	}
	
	var initTpl = function(){
		account_tpl = arttemplateUtil.template.compile($.setHTML('goods/templates/community_price_tpl.html'));
	}
	
	var setData = function(data){
		var res = {
				special_type:param.special_type,
				records:data.records
		}
		$("#pricelist").html(account_tpl(res));
	}
	
	var callback = function(page){
		param.pageNo=page;
		initData();
	}
	
	var init = function() {
		initTpl();
		initData();
		initListBtn();
	};
	
	//页签选择,更换页签需要清空查询条件
	$('#tabs-632335 a').click(function (e) {
		  var tab_value = $(this).attr("tab_value");
		  if(tab_value == 2) {
			  $('#community_name_group').css('display','block');//显示活动小区
			  $('#clear_form_group').show(); //换行符显示
		  } else {
			  $('#community_name_group').css('display','none'); //隐藏活动小区
			  $('#clear_form_group').hide(); //换行符隐藏
		  }
		  param.special_type=tab_value;
		  param.pageNo=1;
		  initData();
	});
	
	//查询
   $('.serchBtn').on('click',function(){
	   if(query_demo.check()) {
		   param.pageNo=1;
		   $.each($('#serchForm').serializeArray(), function(i, item) {
				if(item.value==""){
					delete param[item.name]
				}else{
					param[item.name]=item.value;
				}
			})
			initData();
	   }
	});
   
	//重置
	$('.resetBtn').on('click',function(){
		$.each($('#serchForm').serializeArray(), function(i, item) {
			delete param[item.name];
		});
		$("form[name=serchForm]")[0].reset()
		$("#serchForm input").removeClass("Validform_error");
		$("#serchForm .Validform_checktip").removeClass("Validform_wrong").html("");
		initData();
	});
	
	//导出
	/*$('.excelBtn').on('click',function(){
		$("#excel").attr("action",config.apiRoot()+"/api/seller/v1/exportGoodsPricesList");
		var html = [];
		html.push("<input name=\"special_type\" type='hidden' id=\"special_type\" value=\""+param.special_type+"\" />");
		$.each($('#serchForm').serializeArray(), function(i, item) {
			html.push("<input name=\""+item.name+"\" type='hidden' id=\""+item.name+"\" value=\""+item.value+"\" />");
		});
		$.each(config.setFullParameterArr({}), function(i, item) {
			html.push("<input name=\""+i+"\" type='hidden' id="+i+" value=\""+item+"\" />");
		});
		$("#excel").html(html.join(""));
		$("#excel").submit();
	});*/
	/********************************************验证查询数据********************************************/
	var query_demo = $("#serchForm").Validform({
		tiptype:3,
		isShowRight:false,
		datatype: {
			"numCompare": function(gets,obj,curform,regxp){ //配送次数比较
				var minnum = $.trim($("#serchForm input[name=min_delivery_times]").val());
				var maxnum = $.trim($("#serchForm input[name=max_delivery_times]").val());
				var reg = /^[1-9]\d{1,7}$|^[0-9]$/;
				
				if((stringUtil.isNotEmpty(minnum) && !reg.test(minnum)) || (stringUtil.isNotEmpty(maxnum) && !reg.test(maxnum))) {
					return "配送次数范围为[0-99999999]";
				} 
				
				if(stringUtil.isNotEmpty(minnum) && stringUtil.isNotEmpty(maxnum)) {
					if (parseInt(maxnum) < parseInt(minnum)) {
						return "开始配送次数必须小于结束配送次数";
					}
				} 
				
				$("#serchForm input[name=min_delivery_times]").removeClass("Validform_error");
				$("#serchForm input[name=max_delivery_times]").removeClass("Validform_error");
				return true;
			},"priceCompare": function(gets,obj,curform,regxp){ //价格比较
				var minPrice = $("input[name=min_special_price]").val();
				var maxPrice = $("input[name=max_special_price]").val();
				if((stringUtil.isNotEmpty(minPrice) && !commonUtil.isPrice(minPrice)) || (stringUtil.isNotEmpty(maxPrice) && !commonUtil.isPrice(maxPrice))) {
					return "格式错误,价格范围在【0-99999.99】";
				}
				
				if(stringUtil.isNotEmpty(minPrice) && stringUtil.isNotEmpty(maxPrice)) {
					if (parseFloat(maxPrice) < parseFloat(minPrice)) {
						return "当前价格区间范围错误";
					}
				}
				$("input[name=min_special_price]").removeClass("Validform_error");
				$("input[name=max_special_price]").removeClass("Validform_error");
				return true;
			}
        }
	});
	
	/***********************************************点击删除按钮删除活动价格*********************************/
	var delCommunityPrice = function() {
		$("#pricelist").off('click','.delete_community_price').on('click','.delete_community_price',function(){
			//获取账户id
			var area_id = $(this).attr("data-area-id");
			var goods_id = $(this).attr("data-goods-id");
			layer.confirm('确定删除吗？', {
				  btn: ['确定','取消']
			}, function(){
				var param = [];
				param.push({"name":"goods_id","value":goods_id});
				param.push({"name":"area_ids_arr","value":area_id});
				var $this = $(this);
				$this.attr('disabled',true);
				common.common_ajax_request('/api/seller/v1/deleteCommunityPrice',param,false,function(data,code,msg){
					if(msg.businessCode==0){
						layer.msg("删除成功！");
						initData();
					}else{
						layer.msg(data.businessNote);
					}
				},"","",function(){
					$this.removeAttr('disabled');
				});
			}, function(){});
			
			return false;
			
		});
		
	}
	
	
	//初始化列表按钮
	var initListBtn = function() {
		delCommunityPrice();
	}
	
   init();
});
